package com.dao.Impl;

import com.dao.OrderDao;
import com.entity.Card;
import com.entity.News;
import com.entity.Order;
import com.entity.PageBean;
import com.util.JDBC;
import com.util.TransBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
public class OrderDaoImpl implements OrderDao {
    /**
     * 添加Order
     * @param or
     * @return
     * @throws SQLException
     */
    @Override
    public int add(Order or)  {
            return JDBC.sqlInsert("insert into order(id,orderId,cardId,cardType,price,pay,credit,status,momo,createdTime) values(null,?,?,?,?,?,?,?,?,?,?)", or.getOrderId(), or.getCardId(), or.getCardType(), or.getPrice(), or.getPay(), or.getCredit(), or.getStatus(), or.getMomo(), or.getCreatedTime());
    }

    /**
     *查询所有Order
     * @return
     */
    @Override
    public List<Order> findAll() {
        List<Order> olist=null;
            List<Map> maps= JDBC.selectMap("select id,orderId,cardId,cardType,price,pay,credit,status,momo,createdTime from order");
            olist= TransBean.populate(Card.class,maps);
        return olist;
    }




    @Override
    public List<Order> findSum()  {
        //sql 语句 菜鸡不会打，还要叫龙龙
      //  select MONTH(`order`.createdTime),sum(price) from `order` GROUP BY MONTH(`order`.createdTime) asc
        List<List> lists = JDBC.selectArray("select MONTH(`order`.createdTime),sum(price) " +
                "from `order` GROUP BY MONTH(`order`.createdTime) ");
        List<Order> orderList =  new ArrayList<>();
        for (List o : lists) {
            orderList.add(new Order((Integer) o.get(0),(Double) o.get(1)));

        }
        return orderList;
    }

    @Override
    public int findByIdAuto() {
        List<List> lists = JDBC.selectArray("select max(orderId) from `order`");
        return (Integer) lists.get(0).get(0);
    }

    @Override
    public int insertOrder(Order order) {
        return JDBC.sqlInsert("insert into `order`(orderId,cardId,cardType,price,pay,credit,status,momo,createdTime) " +
                " values(?,?,?,?,?,?,0,?,?)",order.getOrderId(),order.getCardId(),order.getCardType(),
                order.getPrice(),order.getPay(),order.getCredit(),order.getMomo(),order.getCreatedTime());
    }

    @Override
    public void findShow(PageBean pageBean) {
        String sql = "select count(*) from `order` where 1=1 %s ";
        String[] factor = pageBean.getFactor();
        String whereStr = "";
        if(factor!=null){
            for (int i = 0; i < factor.length; i++) {
                whereStr+=" and "+factor[i];
            }
        }
        sql = String.format(sql,whereStr);
        List<List> lists = JDBC.selectArray(sql);
        Integer count=null;
        if(lists!=null){
            count= ((Long) lists.get(0).get(0)).intValue();
        }
        sql = "select id,orderId,cardId,cardType,price,pay,credit,status,momo,createdTime" +
                " from `order` where 1=1 %s limit ?,?";
        sql = String.format(sql,whereStr);
        List<Map> maps = JDBC.selectMap(sql,(pageBean.getPageNo() - 1) * pageBean.getPageSize(),pageBean.getPageSize());
        pageBean.setData(TransBean.populate(Order.class,maps));
        pageBean.setRowCount(count);
    }

    @Override
    public Order findByOrderId(int orderId) {
        List<Map> maps = JDBC.selectMap("select id,orderId,cardId,cardType,price,pay,credit,status,momo,createdTime" +
                " from `order` where orderId=?",orderId);
        List<Order> list = TransBean.populate(Order.class, maps);
        if(maps!=null&&maps.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateOrderStatus(int orderId) {
        return JDBC.sqlUpdate("update `order` set status=1 \n" +
                " where orderId=?",orderId);
    }

    @Override
    public void findConsume(PageBean pageBean, int cardId) {
        String sql="select count(*) from `order` where cardId=?";
        List<List> lists = JDBC.selectArray(sql, cardId);
        int a= ((Long)lists.get(0).get(0)).intValue();
        pageBean.setRowCount(a);//设置行数;
        pageBean.setTotalPage();//设置页数
        sql="select o.orderId,o.cardId,c.name,price,pay,credit,o.status,o.momo,o.createdTime from `order` o,cardtype c where o.cardType = c.id and cardId = ? limit ?,?";
        List<Map> list = JDBC.selectMap(sql, cardId, (pageBean.getPageNo() - 1) * pageBean.getPageSize(), pageBean.getPageSize());

        pageBean.setData(list);

    }


}
